package com.qfoud.edu.dbutils.framwork.result.impl;

import com.qfoud.edu.dbutils.framwork.exception.DbutilsException;
import com.qfoud.edu.dbutils.framwork.result.base.ResultSetHandler;

import java.sql.ResultSet;

/**
 * @author xiaobobo
 * @title: BeanSingleValHandler
 * @projectName cd-java-2303-project-parent
 * @description: 返回单个数据的结果集处理(说明只有一列 并且只有 一行)
 * @date 2023/5/11  10:09
 */
public class BeanSingleValHandler<T> implements ResultSetHandler<T> {

    private Class clazz;

    public BeanSingleValHandler(Class clazz){
         this.clazz=clazz;
    }

    public T handler(ResultSet resultSet) throws Exception {
        int columnCount = resultSet.getMetaData().getColumnCount();
        if(columnCount>1){
            throw new DbutilsException("有多列数据不符合条件....");
        }
        T t =null;
        if(resultSet.next()){
            //说明有值
            t = (T) resultSet.getObject(1);
        }
        if(resultSet.next()){
           throw new DbutilsException("有多行数据不符合条件....");
        }
        //执行到这里说明数据是符合条件的
        return t;
    }
}
